//
//  DAButton.m
//  DAKitTest
//
//  Created by DreamArts on 16/3/31.
//  Copyright © 2016年 DreamArts. All rights reserved.
//

#import "DAButton.h"

static UIButtonType theButtonType;

@interface DAButton (){
  @private
    ButtonTouchBlocks buttonBlock;//点击block
}

@property (nonatomic, assign) CGFloat imageWidth;   // 图片的宽
@property (nonatomic, assign) CGFloat imageHeight;  // 图片的高
@property (nonatomic, assign) CGFloat titleWidth;   // label 的宽
@property (nonatomic, assign) CGFloat titleHeight;  // label 的高


@end

@implementation DAButton

- (instancetype)initWithFrame:(CGRect)frame style:(DAButtonStyle)style{
    self = [super initWithFrame:frame];
    if(self){
        if(style){
            self.style = style;
        }else{
            self.style = DAButtonStyleNone;
        }
    }
    return self;
}

+ (instancetype)buttonWithType:(UIButtonType)buttonType{
    DAButton *button = [super buttonWithType:buttonType];
    button.style = DAButtonStyleNone;
    theButtonType = buttonType;
    return button;
}

- (void)setStyle:(DAButtonStyle)style{
    //当按钮类型不是系统和自定义的时候，无法使用私有风格
    if(theButtonType != UIButtonTypeSystem && theButtonType != UIButtonTypeCustom){
        return;
    }
    switch (style) {
        case DAButtonStyleNone:
            //do nothing
            break;
        case DAButtonStyleOriginal:
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            self.titleLabel.font = [UIFont systemFontOfSize:14];
            [self setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [self.layer setBorderColor:[[UIColor darkGrayColor] CGColor]];
            [self.layer setBorderWidth:1];
            [self.layer setCornerRadius:3];
            self.clipsToBounds = YES;
            break;
        default:
            break;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 图片在上 label 在下
    if (self.buttonAlignment == DAButtonAlignmentImageTopLabelBottom) {
        [self ImageTopLabelBottom];
    }
    // 图片在左, label 在右
    else if (self.buttonAlignment == DAButtonAlignmentImageleftLabelRight){
        [self ImageLeftLabelRight];
    // 图片在右 label 在左
    } else if (self.buttonAlignment == DAButtonAlignmentImageRightLabelLeft){
        [self ImageRightLabelLeft];
    // 图片在下 label 在上
    } else if (self.buttonAlignment == DAButtonAlignmentImageBottomLabelTop){
        [self ImageBottmLabelTop];
    }else if (self.buttonAlignment == DAButtonAlignmentImageCenter){
        
        [self imageIncenter];
    }
    else{
        // 默认
    }
    

    
}

#pragma mark - 图片在上 label 在下
- (void)ImageTopLabelBottom{
    CGFloat ButtonWidth = self.bounds.size.width;         // button 的宽度
    CGFloat ButtonHeight = self.bounds.size.height;       // button 的高度

    // 判断 Button 的width是不是大于 height   如果大于 取最小的边长的一半作为图片的宽和高
    if (ButtonWidth > ButtonHeight) {
        _imageWidth = ButtonHeight / 2;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth;
        _titleHeight = ButtonHeight / 2;
        
        self.imageView.frame = CGRectMake((ButtonWidth - _imageWidth) / 2, 0, _imageWidth, _imageHeight);
        self.titleLabel.frame = CGRectMake(0, (ButtonHeight - _imageHeight), _titleWidth, _titleHeight);
    }
    // 判断 button 的width 和height 的大小 
    else if (ButtonWidth < ButtonHeight) {
        
        _imageWidth = ButtonWidth;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth;
        _titleHeight = _imageHeight;
        
        self.imageView.frame = CGRectMake((ButtonWidth - _imageWidth) / 2, 0, _imageWidth, _imageHeight);
        self.titleLabel.frame = CGRectMake(0, ButtonHeight - _imageHeight, _titleWidth, _titleHeight);

    }
    else{  // button 的宽和高相等的情况下
        
        _imageWidth = ButtonWidth / 2;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth;
        _titleHeight = _imageHeight;
        
        self.imageView.frame = CGRectMake((ButtonWidth - _imageWidth) / 2, 0, _imageWidth, _imageHeight);
        self.titleLabel.frame = CGRectMake(0, ButtonWidth - _imageHeight, _titleWidth, _titleHeight);

        
    }
    
}

#pragma mark - 图片在下 label 在上
- (void)ImageBottmLabelTop{
    CGFloat ButtonWidth = self.bounds.size.width;         // button 的宽
    CGFloat ButtonHeight = self.bounds.size.height;       // button 的高
    
    if (ButtonWidth > ButtonHeight) {             // 判断button 的宽和高的大小 当宽大于高的情况下
        
        _imageWidth = ButtonHeight / 2;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth;
        _titleHeight = ButtonHeight / 2;
        
        self.titleLabel.frame = CGRectMake(0, 0, _titleWidth, _titleHeight);
        self.titleLabel.backgroundColor = [UIColor yellowColor];
        self.imageView.frame = CGRectMake((ButtonWidth - _imageWidth) / 2, ButtonHeight - _titleHeight, _imageWidth, _imageHeight);
        
        
    } else if (ButtonWidth < ButtonHeight){      // 判断button 的宽和高的大小  当宽小于高的情况下
        
        _imageWidth = ButtonWidth;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth;
        _titleHeight = _imageHeight;
        
        self.titleLabel.frame = CGRectMake(0, 0, _titleWidth, _titleHeight);
        self.imageView.frame = CGRectMake((ButtonWidth - _imageWidth) / 2, ButtonHeight - _titleHeight, _imageWidth, _imageHeight);
        
    } else{
//button  宽和高相等的情况下
        _imageWidth = ButtonWidth / 2;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth;
        _titleHeight = _imageHeight;
        
        self.titleLabel.frame = CGRectMake(0, 0, _titleWidth, _titleHeight);
        self.imageView.frame = CGRectMake((ButtonWidth - _imageWidth) / 2, ButtonHeight - _titleHeight, _imageWidth, _imageHeight);
    }
}

#pragma mark - 图片在右 label 在左
- (void)ImageRightLabelLeft{
    
    CGFloat ButtonWidth = self.bounds.size.width;        // button 的宽
    CGFloat ButtonHeight = self.bounds.size.height;      // button 的高
    
    if (ButtonWidth > ButtonHeight) {                   // 判断button  的宽和高 当宽大于高的情况下
        
        _imageWidth = ButtonHeight;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth - _imageWidth;
        _titleHeight = _imageHeight;
        
        self.titleLabel.frame = CGRectMake(0, 0, _titleWidth, _titleHeight);
        self.imageView.frame = CGRectMake(ButtonWidth - _titleWidth, 0, _imageWidth, _imageHeight);

        
    } else if (ButtonWidth < ButtonHeight){             // 判断button 的宽和高 当宽小于高的情况下
        
        _imageWidth = ButtonWidth / 2;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth - _imageWidth;
        _titleHeight = _imageHeight;
        
        self.titleLabel.frame = CGRectMake(0, (ButtonHeight - _titleHeight) / 2, _titleWidth, _titleHeight);
        self.imageView.frame = CGRectMake((ButtonWidth - _titleWidth), (ButtonHeight - _imageHeight) / 2, _imageWidth, _imageHeight);
        
    } else{
        // 当button 的宽和高相等的情况下
        _imageWidth = ButtonWidth / 2;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth - _imageWidth;
        _titleHeight = _imageHeight;
        
        self.titleLabel.frame = CGRectMake(0, (ButtonHeight - _titleHeight) / 2, _titleWidth, _titleHeight);
        self.imageView.frame = CGRectMake(ButtonWidth - _titleWidth, (ButtonHeight - _imageHeight) / 2, _imageWidth, _imageHeight);

        
    }
    
}

#pragma mark - 图片在左, label 在右
- (void)ImageLeftLabelRight{
    
    CGFloat ButtonWidth = self.bounds.size.width;          // button 的宽
    CGFloat ButtonHeight = self.bounds.size.height;        // button 的高
    
    if (ButtonWidth > ButtonHeight) {
                                                           // 判断button 的宽和高 当宽小于高的情况下
        _imageWidth = ButtonHeight;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth - _imageWidth;
        _titleHeight = _imageHeight;
        
        self.imageView.frame = CGRectMake(0, 0, _imageWidth, _imageHeight);
        self.titleLabel.frame = CGRectMake(ButtonWidth - _imageWidth, (ButtonHeight - _titleHeight) / 2, _titleWidth, _imageHeight);

        
    } else if (ButtonWidth < ButtonHeight) {
                                                             // 判断button  的宽和高 当宽小于高的情况下
        _imageWidth = ButtonWidth / 2;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth - _imageWidth;
        _titleHeight = _imageHeight;
        
        self.imageView.frame = CGRectMake(0,( ButtonHeight - _imageHeight) / 2, _imageWidth, _imageHeight);
        self.titleLabel.frame = CGRectMake(ButtonWidth - _imageWidth, (ButtonHeight - _titleHeight) / 2, _titleWidth, _imageHeight);

        
    } else{
                                                             // 当button 的宽和高相等的情况下
        _imageWidth = ButtonWidth / 2;
        _imageHeight = _imageWidth;
        _titleWidth = ButtonWidth - _imageWidth;
        _titleHeight = _imageHeight;
        
        self.imageView.frame = CGRectMake(0, (ButtonHeight - _imageHeight) / 2, _imageWidth, _imageHeight);
        self.titleLabel.frame = CGRectMake(ButtonWidth - _imageWidth, (ButtonHeight - _titleHeight) / 2, _titleWidth, _titleHeight);

        
    }
}

#pragma mark - 图片在中间
- (void)imageIncenter{
    CGFloat ButtonWidth = self.bounds.size.width;    // button 的宽
    CGFloat ButtonHeight = self.bounds.size.height;  // button 的高
    self.imageWidth = 30;
    self.imageHeight = 28;
    
    self.imageView.frame = CGRectMake((ButtonWidth - 30) / 2, (ButtonHeight - 28) /2 , self.imageWidth, self.imageHeight);
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
}

#pragma mark - 拖拽
- (BOOL)dragEnabled
{
    return NO;   // 默认不允许拖拽
}

- (void)setDragEnabled:(BOOL)dragEnabled
{
    if (dragEnabled) {
        self.userInteractionEnabled = YES;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panchAction:)];
        [self addGestureRecognizer:pan];
    } else{
        self.userInteractionEnabled = NO;
    }
}


- (void)panchAction:(UIPanGestureRecognizer *)pan{
    
    DAButton *button = (DAButton *)pan.view;
    CGPoint p = [pan translationInView:button];
    [button setTransform:CGAffineTransformTranslate(button.transform, p.x, p.y)];
    
    [pan setTranslation:CGPointZero inView:button];
    
    
}


#pragma mark - 添加长按手势
- (void)addLongPanWithAnimation:(BOOL)animation
{
    
    // 判断是否开启动画
    if (animation) {
        self.userInteractionEnabled = YES;
        UILongPressGestureRecognizer *longPGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [self addGestureRecognizer:longPGR];
    } else{
        self.userInteractionEnabled = NO;
    }
    
}

// 长按动画
- (void)longPress:(UILongPressGestureRecognizer *)longPress{
    
    // 旋转动画 rotation
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    float top = M_PI / 18;
    float bom = -M_PI / 18;
    keyAnimation.values = @[@(top),@(bom),@(0), @(top)];
    keyAnimation.repeatCount = NSUIntegerMax;
    keyAnimation.duration = 0.3;
    [self.layer addAnimation:keyAnimation forKey:@"key"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 停止动画
        [self.layer removeAnimationForKey:@"key"];
        
    });
    
}

#pragma mark - 需要自己设定长按的动画
- (void)customLongPanAnimation:(SEL)action target:(id)target animation:(BOOL)animation
{
    
    // 判断是否开启动画
    if (animation) {
        UILongPressGestureRecognizer *longPGR = [[UILongPressGestureRecognizer alloc] initWithTarget:target action:action];
        [self addGestureRecognizer:longPGR];
    }
    
}

#pragma mark - 按钮设置及反馈事件
- (void)addButtonInView:(UIView *)superView title:(NSString *)title image:(UIImage *)image DAButtonAlignment:(DAButtonAlignment)alignment onTouchBlock:(ButtonTouchBlocks)block{
    if(title){
        [self setTitle:title forState:UIControlStateNormal];
    }
    if(image){
        [self setImage:image forState:UIControlStateNormal];
    }
    if(alignment){
        self.buttonAlignment = alignment;
    }
    if(superView){
        [superView addSubview:self];
    }

    [self addTarget:self action:@selector(buttonTouchDown) forControlEvents:UIControlEventTouchDown];
    [self addTarget:self action:@selector(buttonTouchDownRepeat) forControlEvents:UIControlEventTouchDownRepeat];
    [self addTarget:self action:@selector(buttonTouchDragInside) forControlEvents:UIControlEventTouchDragInside];
    [self addTarget:self action:@selector(buttonTouchDragOutside) forControlEvents:UIControlEventTouchDragOutside];
    [self addTarget:self action:@selector(buttonTouchDragEnter) forControlEvents:UIControlEventTouchDragEnter];
    [self addTarget:self action:@selector(buttonTouchDragExit) forControlEvents:UIControlEventTouchDragExit];
    [self addTarget:self action:@selector(buttonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [self addTarget:self action:@selector(buttonTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
    [self addTarget:self action:@selector(buttonTouchCancel) forControlEvents:UIControlEventTouchCancel];
    [self addTarget:self action:@selector(buttonValueChanged) forControlEvents:UIControlEventValueChanged];

    buttonBlock = block;
}

#pragma mark - 各种点击情况下的事件
- (void)buttonTouchDown{
    buttonBlock(UIControlEventTouchDown);
}

- (void)buttonTouchDownRepeat{
    buttonBlock(UIControlEventTouchDownRepeat);
}

- (void)buttonTouchDragInside{
    buttonBlock(UIControlEventTouchDragInside);
}

- (void)buttonTouchDragOutside{
    buttonBlock(UIControlEventTouchDragOutside);
}

- (void)buttonTouchDragEnter{
    buttonBlock(UIControlEventTouchDragEnter);
}

- (void)buttonTouchDragExit{
    buttonBlock(UIControlEventTouchDragExit);
}

//点击内部的情况
- (void)buttonTouchUpInside{
    buttonBlock(UIControlEventTouchUpInside);
}

- (void)buttonTouchUpOutside{
    buttonBlock(UIControlEventTouchUpOutside);
}

- (void)buttonTouchCancel{
    buttonBlock(UIControlEventTouchCancel);
}

- (void)buttonValueChanged{
    buttonBlock(UIControlEventValueChanged);
}

@end
